var ajax = new Array();
var blockObjUpdate=true;
function getEle(name)
{
    return document.getElementById(name);
}
function isEmpty(name)
{
    return getEle(name).value=='' || getEle(name).value==null;
}
function selectReset(ele)
{
    getEle(ele).selectedIndex=0;
}
//Client module js functions
function toogle_client()
{
    var rows = getEle('client').getElementsByTagName("tr");
    if(getEle('legal').checked==true) //legal
    {
        getEle('name2').value=getEle('name').value;
        getEle('fp_code2').value=getEle('fp_code').value;
        rows[0].style.display='none';
        rows[1].style.display='table-row';
        rows[3].style.display='none';
        rows[4].style.display='table-row';
        rows[5].style.display='table-row';
        rows[6].style.display='table-row';

    }
    else //individual
    {
        getEle('name').value=getEle('name2').value;
        getEle('fp_code').value=getEle('fp_code2').value;
        rows[0].style.display='table-row';
        rows[1].style.display='none';
        rows[3].style.display='table-row';
        rows[4].style.display='none';
        rows[5].style.display='none';
        rows[6].style.display='none';
    }
}
function toogle_address()
{
    if(getEle('chbox_address').checked==true) //use address
    {
        getEle('real_address').value=getEle('legal_address').value;
        getEle('real_address').disabled=true;
    }
    else //don't use
    {
        getEle('real_address').disabled=false;
    }
}
function write_real()
{
    if(getEle('chbox_address').checked==true)
    {
        getEle('real_address').disabled=false;
        getEle('real_address').value=getEle('legal_address').value;
        getEle('real_address').disabled=true;
    }
}
function client_submit()
{
    getEle('real_address').disabled=false;
}
//Object module js functions
function toogle_object()
{
    var rows = getEle('object').getElementsByTagName("tr");
    if(getEle('object_type').options[getEle('object_type').selectedIndex].value==1) //transport
    {
        rows[4].style.display='table-row';
        rows[5].style.display='table-row';
        rows[6].style.display='table-row';
        rows[7].style.display='table-row';
        rows[8].style.display='table-row';
        rows[9].style.display='table-row';
    }
    else
    {
        rows[4].style.display='none';
        rows[5].style.display='none';
        rows[6].style.display='none';
        rows[7].style.display='none';
        rows[8].style.display='none';
        rows[9].style.display='none';
    }
}
function toogle_status()
{
    if(getEle('status').options[getEle('status').selectedIndex].value==2) //atjaunots?
    {
        getEle('prev_pol').disabled=false;
    }
    else
    {
        getEle('prev_pol').disabled=true;
    }
}
function getObjList(sel,nr)
{
    if(blockObjUpdate) return;
	var type = sel.options[sel.selectedIndex].value;
		
	if(type.length>0){
	   getEle('object'+nr).options.length = 0;
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'ajax/obj.php?obj_type='+type;	
		ajax[index].onCompletion = function(){ createObjects(index,nr) };
		ajax[index].runAJAX();
	}
}

function createObjects(index,nr)
{
	var obj = getEle('object'+nr);
	eval(ajax[index].response);
    changeObjLink('object'+nr, nr);
}
function ins_type_update(check_db)
{
    if(isEmpty('pol_nr'))
    {
        alert('Enter Policy Number');
        selectReset('ins_type');
        getEle('pol_nr').focus();
        return;
    }
    if(check_db)
    {
    var pol_nr=getEle('pol_nr').value;
    var add_pol_nr=getEle('pol_add_nr').value;	
    var index = ajax.length;
    ajax[index] = new sack();
		
    ajax[index].requestFile = 'ajax/check_policy.php?pol_nr='+pol_nr+'&pol_add_nr='+add_pol_nr;	
    ajax[index].onCompletion = function(){ eval(ajax[index].response); };
    ajax[index].runAJAX();
    }
	var rows = getEle('policy_dynamic').getElementsByTagName("tr");
    if(getEle('ins_type').options[getEle('ins_type').selectedIndex].value==2) //OCTA
    {
        rows[0].style.display='none';
        rows[1].style.display='none';
        rows[2].style.display='table-row';
        rows[3].style.display='table-row';
        getEle('cta_premium').value='';
        getEle('ng_premium').value='';
        getEle('persons_count').value='';
    }
    else
    {
        rows[0].style.display='table-row';
        rows[1].style.display='table-row';
        rows[2].style.display='none';
        rows[3].style.display='none';
        selectReset('TL_codes');
        selectReset('octa_premium');
        selectReset('octa_discounts');
        getEle('riska_class').value='';
    }
    //Veselība (5), Nelaimes Gadījumi (6), Palīdzība(16)
    if(getEle('ins_type').options[getEle('ins_type').selectedIndex].value==5 || getEle('ins_type').options[getEle('ins_type').selectedIndex].value==6 || getEle('ins_type').options[getEle('ins_type').selectedIndex].value==16)
    {
        getEle('persons_count').disabled=false;
    }
    else
    {
        getEle('persons_count').disabled=true;
        getEle('persons_count').value='';
    }
}
function changeObjLink(sel, nr)
{
    if(blockObjUpdate) return;
    var ele=getEle(sel);
    var id = ele.options[ele.selectedIndex].value;
    var other_name=sel.name=='object1'?'object2':'object1';
    var other = getEle(other_name).options[getEle(other_name).selectedIndex].value;
    if(id==other)
    {
        alert('Object already selected');
        sel.selectedIndex=0;
        getEle('object_link'+nr).href="";
        return;
    }
	getEle('object_link'+nr).href="?p=obj&a=view&id="+id+"&no_head";	
}
function newWindow(ele,name,width,heigth)
{
    window.open(ele.href, name, 'width='+width+',height='+heigth+',left=200,top=100,scrollbars=yes,resizable=yes');
}
function openWindow(url,name,width,heigth)
{
    window.open(url, name, 'width='+width+',height='+heigth+',left=200,top=100,scrollbars=yes,resizable=yes');
}
function brokerPercChange()
{
    if(!isEmpty('broker_perc'))
        getEle('broker').style.display='block';
    else
        getEle('broker').style.display='none';
}
function newPolicyPost()
{
    if(getEle('ins_comp').selectedIndex==0)
    {
        alert('Please choose insuarance company');
        getEle('ins_comp').focus();
        return;
    }
    if(isEmpty('pol_nr'))
    {
        alert('Please input Policy Number');
        getEle('pol_nr').focus();
        return;
    }
    if(getEle('ins_type').selectedIndex==0)
    {
        alert('Please choose insuarance type');
        getEle('ins_type').focus();
        return;
    }
    if(getEle('taker').selectedIndex==0)
    {
        alert('Please choose insuarance taker');
        getEle('taker').focus();
        return;
    }
    if(getEle('currency').selectedIndex==0)
    {
        alert('Please choose currency');
        getEle('currency').focus();
        return;
    }
    if(isEmpty('ins_prem'))
    {
        alert('Please input Insuarance Premium');
        getEle('ins_prem').focus();
        return;
    }
    if(isEmpty('commision_perc'))
    {
        alert('Please input Commision Percent');
        getEle('commision_perc').focus();
        return;
    }
    if(getEle('employee').selectedIndex==0)
    {
        alert('Please choose Employee');
        getEle('employee').focus();
        return;
    }
    if(isEmpty('date_from'))
    {
        alert('Please input Starting date');
        getEle('date_from').focus();
        return;
    }
    if(isEmpty('date_to'))
    {
        alert('Please input Ending date');
        getEle('date_to').focus();
        return;
    }
    if(isEmpty('payment_count'))
    {
        alert('Please input Payment count');
        getEle('payment_count').focus();
        return;
    }
    if(confirm('Everything is OK! Add new policy?'))
    {
        document.forms["policy_frm"].submit();
    }
}
function toogle_Debug()
{
    var cont=getEle('debug-bar-content');
    var img=getEle('img_debug_toogle');
    if(cont.style.display=='none')
    {
        cont.style.display='block';
        img.src='img/down.png';
    }
    else
    {
        cont.style.display='none';
        img.src='img/up.png';
    }
}
function Redirect(url)
{
    window.location=url;
}
function initEditPolicy()
{
    brokerPercChange();
    ins_type_update(false);
    toogle_status();
    blockObjUpdate=false;
}
function initAddPolicy()
{
    blockObjUpdate=false;
}
function SmileIT(smile,form,text){
document.forms[form].elements[text].value = document.forms[form].elements[text].value+" "+smile+" ";
document.forms[form].elements[text].focus();
}
function PopMoreSmiles(form,name) {
link='moresmiles.php?form='+form+'&text='+name
newWin=window.open(link,'moresmile','height=500,width=450,resizable=no,scrollbars=yes');
if (window.focus) {newWin.focus()}
}
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=740,height=380,left = 340,top = 280');");
}

function chatsubmit() {
setTimeout('document.chbox.reset()',100);
}

function confirm_delete()
{
   if(confirm('Are you sure you want to do this ?'))
   {
        self.location.href='./index.php?p=chat&no_head&delall';
   }
}
function private_reply(to) { 
        parent.document.forms[0].shbox_text.value='/private '+to+' ';
        parent.document.forms[0].shbox_text.focus();
}
function reloadAndClose()
{
    var URL = unescape(window.opener.location.pathname);
    window.opener.location.href = URL;
    window.close();
}
function refresh(url)
{
	window.opener.location.href = url;
	if (window.opener.progressWindow)
	   window.opener.progressWindow.close();
	window.close();
}
function toogleDebugBar()
{
    var ele=getEle('debug-bar');
    var ele2=getEle('show_debug_bar');
    var var_value;
    if(ele.style.display=='block')
    {
        ele.style.display='none';
        ele2.style.display='block';
        var_value=0;
    }
    else
    {
        ele.style.display='block';
        ele2.style.display='none';
        var_value=1;
    }
    var index = ajax.length;
    ajax[index] = new sack();
    ajax[index].requestFile = 'ajax/var.php?var=debug_menu&value='+var_value;
    ajax[index].runAJAX();
}
function report(form)
{
    if(isEmpty('date_from'))
    {
        alert('Date From is required');
        getEle('date_from').focus();
        return false;
    }
    if(isEmpty('date_to'))
    {
        alert('Date To is required');
        getEle('date_to').focus();
        return false;
    }
    document.forms[form].submit();
}
function repEmpChange(same)
{
    selectReset('broker1');
    selectReset('broker2');
    selectReset('ins_takers');
    selectReset('ins_comps');
    if(same=='1')
        getEle('employee2').selectedIndex=getEle('employee1').selectedIndex;

}
function repBrokChange(same)
{
    selectReset('employee1');
    selectReset('employee2');
    selectReset('ins_takers');
    selectReset('ins_comps');
    if(same=='1')
        getEle('broker2').selectedIndex=getEle('broker1').selectedIndex;
}
function repTakersChange()
{
    selectReset('broker1');
    selectReset('broker2');
    selectReset('employee1');
    selectReset('employee2');
    selectReset('ins_comps');
}
function repCompChange()
{
    selectReset('broker1');
    selectReset('broker2');
    selectReset('employee1');
    selectReset('employee2');
    selectReset('ins_takers');
}